﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;

namespace DAL
{
    public class ChiTietDonDatHangQuery:DataProvide
    {
        public List<ChiTietDonDatHangDAL> LayDSChiTietDonDatHang(int maDDH)
        {
            List<ChiTietDonDatHangDAL> dsChiTietDDH = new List<ChiTietDonDatHangDAL>();
            SqlConnection chuoiKetNoi = null;
            try
            {
                base.Connect();
                chuoiKetNoi = connection;
                string sqlString = String.Format(@"SELECT * FROM ChiTietDonDatHang WHERE MaDonDatHang ='"+ maDDH+"'");

                SqlDataReader dr = ExecuteReader(sqlString);
                while (dr.Read())
                {
                    ChiTietDonDatHangDAL ddh = (ChiTietDonDatHangDAL)GetDataRowFromDataReader(dr);                   
                    dsChiTietDDH.Add(ddh);
                }
            }/*
            catch
            {
                return null;
            }*/
            finally
            {
                if (chuoiKetNoi != null && chuoiKetNoi.State == System.Data.ConnectionState.Open)
                    chuoiKetNoi.Close();
            }
            return dsChiTietDDH;
        }


        //them chi tiet don dat hang
        public bool ThemChiTietDonDatHang(ChiTietDonDatHangDAL ctDDH)
        {
            bool ketQua = true;
            try
            {
                base.Connect();

                string sqlString = "INSERT INTO ChiTietDonDatHang VALUES(" +
                    ctDDH.MaDonDatHang + ", N'" +
                    ctDDH.MaSanPham + "', " +
                    ctDDH.SoLuong + ", " +
                    ctDDH.ThanhTien +  ")";

                ExecuteNonQuery(sqlString);
            }/*
            catch (Exception ex)
            {
                ketQua = false;
            }*/
            finally
            {
                if (connection != null && connection.State == System.Data.ConnectionState.Open)
                    Disconnect();
            }
            return ketQua;
        }

        //cap nhat chi tiet don dat hang
        public bool CapNhatChiTietDDH(ChiTietDonDatHangDAL ctDDH,string maSP)
        {
            bool ketQua = true;
            try
            {
                base.Connect();

                string sqlString = "UPDATE ChiTietDonDatHang" +
                    " SET MaSanPham = N'" + ctDDH.MaSanPham+ "', " +
                    " SoLuong = " + ctDDH.SoLuong+ ", " +
                    " ThanhTien = " + ctDDH.ThanhTien +                     
                    " WHERE MaDonDatHang = " + ctDDH.MaDonDatHang + 
                    " and MaSanPham= N'" + maSP+"'";

                ExecuteNonQuery(sqlString);
            }/*
            catch (Exception ex)
            {
                ketQua = false;
            }*/
            finally
            {
                if (connection != null && connection.State == System.Data.ConnectionState.Open)
                    Disconnect();
            }
            return ketQua;
        }

        //xoa chi tiet don dat hang
        public static bool XoaChiTietDonDatHang(string maDDH,string maSP)
        {
            SqlConnection ketNoi = null;
            bool ketQua = true;
            try
            {
                ketNoi = KetNoi();

                string insertCommand = "delete from   ChiTietDonDatHang  WHERE	MaDonDatHang =" + maDDH + " and MaSanPham= N'"+maSP+"'";

                SqlCommand lenh = new SqlCommand(insertCommand, ketNoi);

                lenh.ExecuteNonQuery();


            }
            catch (Exception)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }

        //xoa chi tiet don hang theo don hang
        public static bool XoaChiTietDonDatHangTheoDDH(string maDDH)
        {
            SqlConnection ketNoi = null;
            bool ketQua = true;
            try
            {
                ketNoi = KetNoi();

                string insertCommand = "delete from   ChiTietDonDatHang  WHERE	MaDonDatHang =" + maDDH + " ";

                SqlCommand lenh = new SqlCommand(insertCommand, ketNoi);

                lenh.ExecuteNonQuery();


            }
            catch (Exception)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }



        protected override object GetDataRowFromDataReader(SqlDataReader dr)
        {
            ChiTietDonDatHangDAL dsChiTietDDH = new ChiTietDonDatHangDAL();
            dsChiTietDDH.MaDonDatHang = dr.GetInt32(0);
            dsChiTietDDH.MaSanPham=dr.GetString(1);
            if (!dr.IsDBNull(2))
                dsChiTietDDH.SoLuong = dr.GetInt32(2);
            if (!dr.IsDBNull(3))
                dsChiTietDDH.ThanhTien = float.Parse(dr.GetDouble(3).ToString());
            return (object)dsChiTietDDH;
        
        }
    }
}
